package com.atguigu.tingshu;

import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.model.album.AlbumInfo;
import org.junit.jupiter.api.Test;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @BelongsProject: tingshu-parent
 * @BelongsPackage: com.atguigu.tingshu
 * @Author: jiangyi
 * @CreateTime: 2025-10-15  17:28
 * @Description: TODO
 * @Version: 1.0
 */
@SpringBootTest
public class BloomFliter {

    @Autowired
    private AlbumInfoMapper albumInfoMapper;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void test() {
        // 创建 bloomFilter ，且存储元素类型为 Long
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter("albumInfo:bloomFilter");
        bloomFilter.tryInit(1000000, 0.01); // 使用更大的容量
        System.out.println("bloomFilter 创建成功");

        // 分页查询专辑信息，避免一次性加载过多数据
        List<AlbumInfo> albumInfos = albumInfoMapper.selectList(null);

        int count = 0;
        for (AlbumInfo albumInfo : albumInfos) {
            if (albumInfo != null && albumInfo.getId() != null) {
                bloomFilter.add(albumInfo.getId());
                count++;
            }
        }

        System.out.println("成功添加 " + count + " 个专辑ID到布隆过滤器");
    }

    @Test
    public void test2() {
        // 创建 bitmap
        redisTemplate.opsForValue().setBit("albumInfo:bitmap", 1619, true);

    }

}
